export default {
  namespaced: true,
  state() {
    return {
      userInfo: uni.getStorageSync('userInfo') || {},
      code: uni.getStorageSync('code') || '',
      openId: uni.getStorageSync('openId') || '',
    }
  },
  mutations: {
    setUserInfo(state, data) {
      uni.setStorageSync('userInfo', data)
      state.userInfo = data
    },
    setCode(state, data) {
      uni.setStorageSync('code', data)
      state.code = data
    },
    setOpenId(state, data) {
      uni.setStorageSync('openId', data)
      state.openId = data
    },
  },
  actions: {
    uniLogin({ commit }) {
      return new Promise((resolve, reject) => {
        uni.login({
          success(res) {
            commit('setCode', res.code)
            resolve()
          },
          fail() {
            commit('setCode', '')
            uni.showToast({
              title: '获取code失败，请稍后重试',
              icon: 'none',
            })
            reject()
          },
        })
      })
    },
  },
}
